function Tools() {
    var that = this;
    this.keyFuncs = ['keyUp', 'keyDown', 'keyLeft', 'keyRight', 'keyEnter', 'keyBack', 'keyPageDown',
        'keyPageUp'
    ];
    this.getFocus = function () {
        that.keyFuncs.forEach(function (v, i) {
            window['old' + v] = window[v];
            if (this.hasOwnProperty(v)) {
                window[v] = this[v]
            } else {
                window[v] = undefined;
            }
        }, this)
    }
    this.storeControl = function () {
        that.keyFuncs.forEach(function (v, i) {
            window['old' + v] = window[v];
        }, this)
    }
    this.recallControl = function () {
        that.keyFuncs.forEach(function (v, i) {
            window[v] = window['old' + v];
        }, this)
    }
    this.loseFocus = function () {
        that.keyFuncs.forEach(function (v, i) {
            window[v] = undefined;
        }, this)
    }
    this.checkEnv = function(){//检测环境
        var result='';
        if(typeof(Promise)!='function'){
            result+='miss Promise,'
        }
        if(typeof(redom)!='object'){
            result+='miss redom,'
        }
        if(typeof(jQuery)!='function'){
            result+='miss jQuery,'
        }
        return result;
    }
    this.getQueryString = function (url, name) { //获取url中的参数
        if (url == null || url == undefined)
            url = location.href
        var querys = {};
        if (url.match(/([^?=&]+)(=([^&]*))/g)) {
            url.match(/([^?=&]+)(=([^&]*))/g).forEach(function (v) {
                querys[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1);
            });
            if (name != undefined || name != null)
                return querys[name];
            else
                return querys;
        } else {
            return undefined;
        }
    };
    this.deepCopy = function (source) { //深度拷贝
        var ret, key, i;
        if ($.type(source) === 'object') {
            ret = {};
            for (key in source) {
                ret[key] = deepCopy(source[key]);
            }
        } else if ($.type(source) === 'array') {
            ret = [];
            for (i = 0; i < source.length; i++) {
                ret[i] = deepCopy(source[i]);
            }
        } else {
            ret = source;
        }
        return ret;
    };
    
    this.scrollNameLeft = function (_ele, _name, _speed) { //文字滚动
        if (_speed == null)
            _speed = 60;
        if(_ele.clientWidth<_ele.scrollWidth){
            var scroll_begin = redom.el('span', {
                style: {
                    paddingRight: '2em'
                }
            });
            scroll_begin.innerHTML = _name;
            var scroll_end = redom.el('span', {
                style: {
                    paddingRight: '2em'
                }
            });
            if(getComputedStyle(_ele).textOverflow=='ellipsis'){
                _ele.style.textOverflow='initial';
            }
            scroll_end.innerHTML = _name;
            var scroll_div = _ele;
            scroll_div.innerHTML = "";
            scroll_div.appendChild(scroll_begin);
            scroll_div.appendChild(scroll_end);
    
            function Marquee() {
                if (scroll_end.offsetWidth - scroll_div.scrollLeft <= 0) {
                    scroll_div.scrollLeft -= scroll_begin.offsetWidth;
                } else {
                    scroll_div.scrollLeft += 3;
                }
            }
            return {timer:setInterval(Marquee, _speed),clear:function(){
                _ele.innerHTML=_name;
                _ele.style.textOverflow='ellipsis';
                _ele.scrollLeft=0;
            }}
        }else{
            return undefined;
        }
    };
    this.strLength = function (str) { //判断字符串长度,中文自动算为两个字符长度
        var a = 0;
        for (var i = 0; i < str.length; i++) {
            if (str.charCodeAt(i) > 255)
                a += 2; //按照预期计数增加2
            else
                a++;
        }
        return a;
    };
    
    this.loadJS = function (url, fn) { //加载js文件到顶部
        var ss = document.getElementsByName('script'),
            loaded = false;
        for (var i = 0, len = ss.length; i < len; i++) {
            if (ss[i].src && ss[i].getAttribute('src') == url) {
                loaded = true;
                break;
            }
        }
        if (loaded) {
            if (fn && typeof fn != 'undefined' && fn instanceof Function) fn();
            return false;
        }
        var s = document.createElement('script'),
            b = false;
        s.setAttribute('type', 'text/javascript');
        s.setAttribute('src', url + '?v=' + new Date().getTime());
        s.onload = s.onreadystatechange = function () {
            if (!b && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
                b = true;
                if (fn && typeof fn != 'undefined' && fn instanceof Function) fn();
            }
        };
        document.getElementsByTagName('head')[0].appendChild(s);
    };
    this.clearHeadJs = function (src) { //清空 LoadJS 加载到顶部的js引用
        var js = document.getElementsByTagName('head')[0].children;
        var obj = null;
        for (var i = 0; i < js.length; i++) {
            if (js[i].tagName.toLowerCase() == "script" && js[i].attributes['src'].value.indexOf(src) > 0) {
                obj = js[i];
            }
        }
        document.getElementsByTagName('head')[0].removeChild(obj);
    };
}

function IPTVElement(_config) {
    var that = this;
    this.el = redom.el('div');
    this.config = _config || {};
    this.focusable = true; //元素是否可落焦
    this.father = (_config && _config.father) || null;
    Object.getOwnPropertyNames(this.config).forEach(function(key,index){
        var element = that.config[key];
        switch(key){
            case 'focusable':
                that[key]=element;
                break;
            case 'id':
                that.el.id=element;
                break;
            case 'focusArray':
            case 'children':
                that[key]=element;
                break;
            case 'class':
                that.el.className=element;
                break;
            case 'style':
                that[key]=element;
                redom.setStyle(this.el,style);
                break;
        }
    })
}
/* _config{
    title: 剧集名 |必传
    poster: 海报 |必传
    code: 全局编码 |必传
    vfrom: |必传
    button: 海报上的播放按钮 默认为true| 非必传
    vtype1: |非必传
    vtype2: |非必传
    returnUrl: |非必传
    children: 组件内加入的其他非焦点元素 类型为HTMLElement数组 默认为[] |非必传
    id: 设置div id |非必传
    class: 设置div class |非必传 
    focusable: 默认true 设置为可落焦元素 |非必传
    father: 设置父级对象 类型为对象 |非必传
    onFocus: 替换默认落焦 |非必传
    freeFocus: 替换默认失焦 |非必传
    keyEnter: 替换默认keyEnter |非必传
    //focusArray: 组件内加入的其他可落焦元素 类型为对象数组 默认为[] |非必传
    
} */
function Movie(_config){
    IPTVElement.call(this,_config);
    var that = this;
    this.el.className = 'movie';
    this.divName=redom.el(".movie_name",{name:'name'},this.config.title);
    this.divPlayButton = redom.el('.movie_playButton',{name:'playButton'},[redom.el('img',{src:'epg/baseImage/playButton/playButton-wing1.png'}),redom.el('img',{src:'epg/baseImage/playButton/playButton-wing2.png'}),redom.el('img',{src:'epg/baseImage/playButton/playButton.png'})])
    this.divPoster = redom.el('img.poster',{name:'poster',src:this.config.poster || '#'});
    redom.setChildren(this.el,[this.divPoster,(_config.button==false?false:true) && this.divPlayButton,this.divName]);
    if(_config.children instanceof Array){
        _config.children.forEach(function(ele){
            if(ele instanceof HTMLElement){
                redom.mount(that.el,ele);
            }else if(ele.el instanceof HTMLElement){
                redom.mount(that.el,ele.el);
            }
        });
    }
    this.onFocus = function(){
        if(_config.onFocus){
            _config.onFocus.apply(this)
        }else{
            this.el.classList.add('movieFocus');
            if(tools){
                that.textMarquee = tools.scrollNameLeft(that.divName,that.divName.innerHTML);
            }
            if(this.el.children.namedItem('playButton')){
                this.el.children.namedItem('playButton').style.display='block';
                this.el.children.namedItem('playButton').children.item(0).className="animate__animated animate__rotate animate__slower animate__infinite infinite";
                this.el.children.namedItem('playButton').children.item(1).className="animate__animated animate__rotate animate__slower animate__infinite infinite";
            }
        }        
    };
    this.freeFocus = function(){
        if(_config.freeFocus){
            _config.freeFocus.apply(this);
        }else{
            that.el.className = "movie";
            if(that.textMarquee){
                that.textMarquee.clear();
            }
            if(this.el.children.namedItem('playButton')){
                this.el.children.namedItem('playButton').style.display='none';
                this.el.children.namedItem('playButton').children.item(0).className="";
                this.el.children.namedItem('playButton').children.item(1).className="";
            }
        }
    };
    this.keyEnter = function(){
        if(_config.keyEnter){
            _config.keyEnter.apply(this);
        }else{
            if(that.config.code){
                
                var url="vod_detail.html?mediaCode="+that.config.code+"&vfrom="+encodeURIComponent(that.config.vfrom||location.pathname)+"&vtype1="+encodeURIComponent(that.config.vtype1)+"&vtype2="+encodeURIComponent(that.config.vtype2)+((that.config.returnUrl!=undefined)?"&returnUrl="+that.config.returnUrl:"");
                window.location.href=url;
                
            }else{
                console.log('miss code')
            }
        }
    };
};